package mq.rabbitmq.topics;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeoutException;

public class Topics {
    public final static String topic_key_1 = "*.orange.*";
    public final static String topic_key_2 = "*.*.rabbit";
    public final static String topic_key_3 = "lazy.#";
    public final static String EXCHANGE_NAME = "topics_logs";
    public final static String HOST = "47.101.147.181";
    public final static String USERNAME = "root";
    public final static String PASSWORD = "root";
    public final static ConnectionFactory factory = new ConnectionFactory();
    static {
        factory.setHost(HOST);
        factory.setUsername(USERNAME);
        factory.setPassword(PASSWORD);
    }
    public static void publicMsg() throws IOException, TimeoutException, InterruptedException {
        List<String> msgList = new ArrayList<>();
        msgList.add("quick.orange.rabbit");
        msgList.add("lazy.orange.elephant");
        msgList.add("quick.orange.fox");
        msgList.add("lazy.brown.fox");
        msgList.add("lazy.pink.rabbit");
        msgList.add("quick.brown.fox");
        msgList.add("lazy.orange.male.rabbit");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        int j = 0;
        while (j<msgList.size()-1){
            String msg = msgList.get(j);
            channel.basicPublish(EXCHANGE_NAME,msg,null,msg.getBytes());
            System.out.printf("发送消息：%s%n",msg);
            Thread.sleep(100);
            j++;
        }
    }

    public static void subscribe(String[] routerKey) throws IOException, TimeoutException {
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        String queueName = channel.queueDeclare().getQueue();
        for (String key:routerKey){
            channel.queueBind(queueName,EXCHANGE_NAME,key);
        }
        channel.basicQos(1);
        channel.basicConsume(queueName,false,(consumerTag, message) -> {
            System.out.printf("订阅队列%s:%s%n",queueName,new String(message.getBody(),"UTF-8"));
            channel.basicAck(message.getEnvelope().getDeliveryTag(),true);
        },consumerTag ->{});

    }

    public static void main(String[] args) {
        new Thread(()->{
            try {
                publicMsg();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TimeoutException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

    }

}
